#include <myheader_c_plus.h>
using std::endl;
using std::cout;
class Base{
public:

    //无参构造函数
    Base()
    :_pbase(new char[1]())
    {
        cout << "Base()" << endl;
    }


    //构造函数
    Base(const char *pbase)
    :_pbase(new char[strlen(pbase) + 1]())
    {
        strcpy(_pbase, pbase);
        cout << "Base(const char*)" << endl;
    }


    //拷贝构造函数
    Base(const Base & rhs)
    :_pbase(new char[strlen(rhs._pbase) + 1]())
    {
        strcpy(_pbase, rhs._pbase);
    }


    
    //析构函数
    ~Base(){
        if(_pbase){
            delete [] _pbase;
            _pbase = nullptr;
        }
        cout << "~Base()" << endl;
    }


    //赋值运算符函数
    Base & operator=(const Base & rhs){
        if(this != &rhs){
            delete [] _pbase;
            _pbase = new char[strlen(rhs._pbase) + 1]();
            strcpy(_pbase, rhs._pbase);
        }
        return *this;
    }


    //获取 char * _pbase
    char * getPbase() const{
        return _pbase;
    }



private:
    char * _pbase;
};

class Drived : public Base{
public:

    //无参构造函数
    Drived(){
        cout << "Drived()" << endl;
    }

    //构造函数
    Drived(const char * pbase)
    :Base(pbase)
    {
        cout << "Drived(const char*)" << endl;
    }


    //拷贝构造函数
    Drived(const Drived & rhs)
    :Base(rhs.getPbase())
    {
    

    }

    //析构函数
    ~Drived(){
        cout << "~Drived" << endl;
    }


    //赋值运算符函数
    Drived & operator=(const Drived & rhs){
        if(this != &rhs){
            //调用基类的赋值运算函数
            Base::operator=(rhs);
        }
        return *this;
    }

};
int main(int argc, char *argv[])
{
    Drived d("hello");
    cout << d.getPbase() << endl;
    Drived d1("world");
    cout << d1.getPbase() << endl;
    d1 = d;
    cout << d1.getPbase() << endl;
    Drived d2 = d;
    cout << d2.getPbase() << endl;
    return 0;
}

